package com.wodogs.utils.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.lang3.StringUtils;

import com.wodogs.utils.bean.WfProxy;
import com.wodogs.utils.service.ProxyService;

public class ProxyCheck {

	public static void main(String[] args) {
//		try {
//			List<String> list = new ArrayList<>();
//			for (int i = 0; i < 100; i++) {
//				list.add(i + ",");
//			}
//			
//			System.out.println(new Test2().list2Str(list, 5));
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}

	public List<WfProxy> process(List<Map<String,Object>> list,final String test_url, final String test_str, int threads) throws Exception {
		List<WfProxy> pList = new ArrayList<WfProxy>();
		if (list == null || list.isEmpty()) {
			return pList;
		}
		
		int size = list.size();
		
		int count = size / threads;
		if(count<1){
			count = 1;
			threads = size;
		}
		ExecutorService executorService = Executors.newFixedThreadPool(threads);
		List<Future<List<WfProxy>>> futures = new ArrayList<Future<List<WfProxy>>>(threads);
		final int nThreads = threads;
		for (int i = 0; i < nThreads; i++) {
			int engIndex = count * (i + 1);
			if(engIndex>size){
				engIndex = size;
			}
			final List<Map<String,Object>> subList = list.subList(count * i, engIndex);
			Callable<List<WfProxy>> task = new Callable<List<WfProxy>>() {
				@Override
				public List<WfProxy> call() throws Exception {
					List<WfProxy> list = new ArrayList<WfProxy>();
					for (Map<String,Object> map : subList) {
						Object id = map.get("id");
						Object ip = map.get("ip");
						Object port = map.get("port");
						Object is_used = map.get("is_used");
						WfProxy p = new WfProxy(id, ip, port,is_used);
						WriteLog.write("mercury test!");
						boolean test = test(p.getIp(), p.getPort(), test_url,test_str);
						if(test){
							list.add(p);
							WriteLog.write(p.getIp()+":" +p.getPort()+" pass!");
						}else{
							ProxyService.disabled(id.toString());
							WriteLog.write(p.getIp()+":" +p.getPort()+" fail!");
						}
					}
					return list;
				}
			};
			futures.add(executorService.submit(task));
		}
		for (Future<List<WfProxy>> future : futures) {
			pList.addAll(future.get());
		}
		executorService.shutdown();
		
		if(pList.size()==0){
			return null;
		}
		return pList;
	}
	
	private boolean test(String ip,String port,String test_url,String test_str){
		try{
			WfProxy wfProxy = new WfProxy();
			wfProxy.setIp(ip);
			wfProxy.setPort(port);
			String str = HttpMry.get(test_url, new HashMap<String, String>(), new HashMap<String, String>(),wfProxy,500,3000);
			if(StringUtils.isNotBlank(str)){
				if(StringUtils.isNotBlank(test_str)&&str.indexOf(test_str)>-1){
					return true;
				}
			}
		}catch(Exception ex){
			return false;
		}
		return false;
	}
}
